package com.gqj.meter.controller;

import com.alibaba.fastjson.JSON;
import com.gqj.common.annotation.Log;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.poi.ExcelUtil;
import com.gqj.meter.domain.MdWhInfo;
import com.gqj.meter.domain.vo.MdWhInfoVo;
import com.gqj.meter.service.IMdWhInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 库房信息Controller
 *
 * @author lxq
 * @date lxq-01-09
 */
@Api(value = "/meter/mdWhInfo", description = "库房信息管理")
@RestController
@RequestMapping("/meter/mdWhInfo")
public class MdWhInfoController extends BaseController {
    @Autowired
    private IMdWhInfoService mdWhInfoService;
    @Value("${downloadPath}")
    private String downloadPath;

    /**
     * 查询库房信息列表
     */
    @ApiOperation("查询库房信息列表")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdWhInfo mdWhInfo) {
        startPage();
        List<MdWhInfo> list = mdWhInfoService.selectMdWhInfoList(mdWhInfo);
        return getDataTable(list);
    }

    /**
     * 导出库房信息列表
     */
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:export')")
    @Log(title = "库房信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdWhInfo mdWhInfo) {
        List<MdWhInfo> list = mdWhInfoService.selectMdWhInfoList(mdWhInfo);
        ExcelUtil<MdWhInfo> util = new ExcelUtil<MdWhInfo>(MdWhInfo.class);
        util.exportExcel(response, list, "库房信息数据");
    }

    /**
     * 获取库房信息详细信息
     */
    @ApiOperation("获取库房信息详细信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:query')")
    @GetMapping(value = "/{whId}")
    public AjaxResult getInfo(@PathVariable("whId") Long whId) {
        return success(mdWhInfoService.selectMdWhInfoByWhId(whId));
    }

    /**
     * 新增库房信息
     */
    @ApiOperation("新增库房信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:add')")
    @Log(title = "库房信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdWhInfo mdWhInfo) {
        return toAjax(mdWhInfoService.insertMdWhInfo(mdWhInfo));
    }

    /**
     * 修改库房信息
     */
    @ApiOperation("修改库房信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:edit')")
    @Log(title = "库房信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdWhInfo mdWhInfo) {
        return toAjax(mdWhInfoService.updateMdWhInfo(mdWhInfo));
    }

    /**
     * 删除库房信息
     */
    @ApiOperation("删除库房信息")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:remove')")
    @Log(title = "库房信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{whIds}")
    public AjaxResult remove(@PathVariable Long[] whIds) {
        return toAjax(mdWhInfoService.deleteMdWhInfoByWhIds(whIds));
    }

    /**
     * 查询库房下拉框
     */
    @ApiOperation("查询库房下拉框")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:mdWhInfoOptions')")
    @GetMapping("/mdWhInfoOptions")
    public AjaxResult mdWhInfoOptions() {
        List<Map<String, Object>> list = mdWhInfoService.mdWhInfoOptions();
        return AjaxResult.success(list);
    }

    /**
     * 下载库房json文件
     */
    @ApiOperation("下载库房json文件")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:downloadJsonMdWhInfo')")
    @GetMapping("/downloadJsonMdWhInfo")
    public AjaxResult downloadJsonMdWhInfo(MdWhInfo mdWhInfo) {
        List<MdWhInfo> wif = mdWhInfoService.selectMdWhInfoList(mdWhInfo);
        List<MdWhInfoVo> vos = StorageUtil.copyList(wif, MdWhInfoVo::new);
        String orderNo = StorageUtil.getOrderNoTo12();
        // 生成文件名
        String fileName = downloadPath;
        String jsonStringAc = JSON.toJSONString(vos);
        StorageUtil.createJsonFile(jsonStringAc, fileName, "库房数据" + orderNo);
        return AjaxResult.success(fileName + "库房数据" + orderNo);
    }

    /**
     * 导入库房execl文件
     */
    @ApiOperation("导入库房execl文件")
    @Log(title = "导入库房execl文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:importDataMdWhInfo')")
    @PostMapping("/importDataMdWhInfo")
    public AjaxResult importDataMdWhInfo(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MdWhInfo> util = new ExcelUtil<MdWhInfo>(MdWhInfo.class);
        List<MdWhInfo> mdWhInfoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = mdWhInfoService.importMdWhInfo(mdWhInfoList, updateSupport, operName);
        return success(message);
    }

    @ApiOperation("下载库房模板")
    @PostMapping("/importTemplateMdWhInfo")
    @PreAuthorize("@ss.hasPermi('storage:mdWhInfo:importTemplateMdWhInfo')")
    public void importTemplateMdWhInfo(HttpServletResponse response) {
        ExcelUtil<MdWhInfo> util = new ExcelUtil<MdWhInfo>(MdWhInfo.class);
        util.importTemplateExcel(response, "库房数据");
    }
}
